home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-04 | 11.0 KB | 495 lines | [TEXT/MPS ] |
- Perl -Sx "{0}" {"Parameters"}; Exit
- #!perl
-
- while ($arg = shift @ARGV) {
- if ($arg eq "-delay") {
- $delay = 1;
- } elsif ($arg eq "-docanchor") {
- $anchordist = shift @ARGV;
- } else {
- die "This can't happen -- $1 passed to noidx";
- }
- }
-
- $[ = 1; # set array base to 1
- $, = ' '; # set output field separator
- $\ = "\n"; # set output record separator
-
- $curfile = 'standard input?';
- $lastchunkbegin = 'never any chunks?';
- $allchunks{0} = 0;
- $allidents{0} = 0;
- $indexlabels{0} = 0;
- $defanchors{0} = 0;
- $uses{0} = 0;
- $anchorlabel{0} = 0;
- $indexanchorlabel{0} = 0;
- $thesedefns{0} = 0;
- $theseuses{0} = 0;
- $defcount{0} = 0;
- $udlist{0} = 0;
- $uidtable{0} = 0;
- $keycounts{0} = 0;
- $sorted{0} = 0;
- $sortkeys{0} = 0;
- $alphacodes = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
- $alphacodelen = length($alphacodes);
- $nextline = 0;
-
- while (<>) {
- chop; # strip record separator
- if (/^\@file /) {
- $curfile = &uniqueid(substr($_, 7, 999999));
- }
- if (/^\@begin /) {
- $lastchunkbegin = $_;
- }
- if (/^\@end docs /) {
- if ($anchordist > 0) {
- $n = $anchordist;
- $lastanchorlabel = &newdocslabel();
- for ($i = $nextline - 1; $i >= 0; $i--) {
- if ($n == 0 || $lines{$i} =~ /^\@begin docs /) {
- &insertafter($i, '@xref label ' . $lastanchorlabel);
- $i = -1;# cause loop to terminate
-
- ;
- }
- elsif ($lines{$i} eq '@nl') {
- $n--;
- }
- }
- }
- }
- if (/^\@end code /) {
- $lastanchorlabel = '';
- }
- if (/^\@defn /) {
- $arg = substr($_, 7, 999999);
- $allchunks{$arg} = 1;
- $lastdefnlabel = &newdefnlabel($arg);
- &slipin('@xref label ' . $lastdefnlabel);
- if ($lastanchorlabel eq '') {
- $lastanchorlabel = $lastdefnlabel;
- }
- if ($anchorlabel{$arg} eq '') {
- $anchorlabel{$arg} = $lastanchorlabel;
- }
- &addlabel(*defanchors, $arg, $lastanchorlabel);
- &addud(*chunkud, 'defn', $arg, $lastanchorlabel);
- $thisusecount = 0;
- }
- if (/^\@use /) {
- $arg = substr($_, 6, 999999);
- $allchunks{$arg} = 1;
- &slipin('@xref label ' . $lastdefnlabel . '-u' . (++$thisusecount));
- &addlabel(*uses, $arg, $lastanchorlabel);
- &addud(*chunkud, 'use', $arg, $lastanchorlabel);
- }
- if (/^\@index use /) {
- $arg = substr($_, 12, 999999);
- $allidents{$arg} = 1;
- if ($lastanchorlabel ne '') {
- &addud(*indexud, 'use', $arg, $lastanchorlabel);
- }
- }
- if (/^\@index defn /) {
- $arg = substr($_, 13, 999999);
- $allidents{$arg} = 1;
- if ($lastanchorlabel ne '') {
- $l = $lastanchorlabel;
- }
- else {
- $l = &newdocslabel();
- &slipin('@xref label ' . $l);
- }
- &addud(*indexud, 'defn', $arg, $l);
- if ($indexanchorlabel{$arg} eq '') {
- $indexanchorlabel{$arg} = $l;
- }
- &slipin('@xref ref ' . $l);# bug fix
- }
- if (/^\@index localdefn /) {
- $arg = substr($_, 18, 999999);
- $allidents{$arg} = 1;
- if ($lastanchorlabel ne '') {
- $l = $lastanchorlabel;
- }
- else {
- $l = &newdocslabel();
- &slipin('@xref label ' . $l);
- }
- &addud(*indexud, 'defn', $arg, $l);
- if ($indexanchorlabel{$arg} eq '') {
- $indexanchorlabel{$arg} = $l;
- }
- &slipin('@xref ref ' . $l);# bug fix
- }
- $lines{$nextline} = $_;
- $nextline++;
- }
-
- for ($i = 0; $i < $nextline; $i++) {
- $line = $lines{$i};
- if ($line =~ /^\@begin /) {
- if ($delay && $lastchunkbegin eq $line) {
- print '@nl';
- print '@nl';
- &lognowebchunks();
- &lognowebindex();
- }
- print $line;
- foreach $X (keys %thesedefns) {
- delete $thesedefns{$X};
- }
- foreach $X (keys %theseuses) {
- delete $theseuses{$X};
- }
- $thischunk = '';
- }
- elsif ($line =~ /^\@defn /) {
- $thischunk = substr($line, 7, 999999);
- printf "\@xref ref %s\n", $anchorlabel{$thischunk};
- print $line;
- }
- elsif ($line =~ /^\@use /) {
- $arg = substr($line, 6, 999999);
- printf "\@xref ref %s\n",
-
- ($anchorlabel{$arg} eq '' ? 'nw\@notdef' : $anchorlabel{$arg});
- print $line;
- }
- elsif ($line =~ /^\@index defn /) {
- $arg = substr($line, 13, 999999);
- $thesedefns{$arg} = 1;
- # no xref ref because of bug fix
- # if (indexanchorlabel[arg] != "")
- # printf "\@xref ref %s\n", indexanchorlabel[arg]
- print $line;
- }
- elsif ($line =~ /^\@index localdefn /) {
- $arg = substr($line, 18, 999999);
- $thesedefns{$arg} = 1;
- # no xref ref because of bug fix
- # if (indexanchorlabel[arg] != "")
- # printf "\@xref ref %s\n", indexanchorlabel[arg]
- print $line;
- }
- elsif ($line =~ /^\@index use /) {
- $arg = substr($line, 12, 999999);
- $theseuses{$arg} = 1;
- if ($indexanchorlabel{$arg} ne '') {
- printf "\@xref ref %s\n", $indexanchorlabel{$arg};
- }
- print $line;
- }
- elsif ($line =~ /^\@end code/) {
- $defout{$thischunk}++;
- foreach $X (keys %thesedefns) {
- delete $theseuses{$X};
- }
- delete $thesedefns{0};
- $n = &alphasort(*thesedefns);
- if ($n > 0) {
- print '@index begindefs';
- for ($j = 0; $j < $n; $j++) {
- $M = (@a = split(' ', $indexud{$sorted{$j}}));
- for ($k = 1; $k <= $M; $k++) {
- if ($a[$k] =~ /^use/) {
- printf "\@index isused %s\n", substr($a[$k], 5,
-
- length($a[$k]) - 5);
- }
- }
- printf "\@index defitem %s\n", $sorted{$j};
- delete $sorted{$j};
- }
- print '@index enddefs';
- }
- delete $theseuses{0};
- $n = &alphasort(*theseuses);
- if ($n > 0) {
- print '@index beginuses';
- for ($j = 0; $j < $n; $j++) {
- $M = (@a = split(' ', $indexud{$sorted{$j}}));
- for ($k = 1; $k <= $M; $k++) {
- if ($a[$k] =~ /^defn/) {
- printf "\@index isdefined %s\n", substr($a[$k], 6,
-
- length($a[$k]) - 6);
- }
- }
- printf "\@index useitem %s\n", $sorted{$j};
- delete $sorted{$j};
- }
- print '@index enduses';
- }
- if ($defout{$thischunk} == 1) {
- if ($defcount{$thischunk} > 1) {
- print '@xref begindefs';
- $n = (@a = split(' ', $defanchors{$thischunk}));
- for ($j = 2; $j <= $n; $j++) {
- printf "\@xref defitem %s\n", $a[$j];
- }
- print '@xref enddefs';
- }
- if ($uses{$thischunk} ne '') {
- print '@xref beginuses';
- $n = (@a = split(' ', $uses{$thischunk}));
- for ($j = 1; $j <= $n; $j++) {
- printf "\@xref useitem %s\n", $a[$j];
- }
- print '@xref enduses';
- }
- else {
- printf "\@xref notused %s\n", $thischunk;
- }
- }
- if ($defout{$thischunk} > 1) {
- printf "\@xref prevdef %s\n", &listget($defanchors{$thischunk},
-
- $defout{$thischunk} - 1);
- }
- if ($defout{$thischunk} < $defcount{$thischunk}) {
- printf "\@xref nextdef %s\n", &listget($defanchors{$thischunk},
-
- $defout{$thischunk} + 1);
- }
- print $line;
- }
- elsif ($line =~ /^\@text /) {
- # grotesque hack to get indexes in HTML
- if ($thischunk eq '') {
- # docs mode
- $arg = substr($line, 7, 999999);
- if ($arg eq '<nowebchunks>') {
- &lognowebchunks();
- }
- elsif ($arg eq '<nowebindex>') {
- &lognowebindex();
- }
- else {
- print $line;
- }
- }
- else {
- print $line;
- }
- }
- else {
- print $line;
- }
- delete $lines{$i};
- }
- if (!$delay) {
- print '@nl';
- print '@nl';
- &lognowebchunks();
- &lognowebindex();
- }
-
- sub insertafter {
- local($i, $S, $n) = @_;
- for ($n = $nextline++; $n - 1 > $i; $n--) {
- $lines{$n} = $lines{$n - 1};
- }
- $lines{$n} = $S;
- }
-
- sub slipin {
- local($S) = @_;
- $lines{$nextline++} = $S;
- }
-
- sub newdefnlabel {
- local($arg, $label) = @_;
- $defcount{$arg} = $defcount{$arg} + 1;
- $label = 'NW' . $curfile . '-' . &uniqueid($arg) . '-' .
-
- &alphacode($defcount{$arg});
- $label;
- }
-
- sub newdocslabel {
- $newdocslabelcount++;
- 'NWD' . &alphacode($newdocslabelcount);
- }
-
- sub addlabel {
- local(*tbl, $arg, $label, $marker) = @_;
- $marker = ' ' . $label;
- if (!&tailmatch($tbl{$arg}, $marker)) {
- $tbl{$arg} = $tbl{$arg} . $marker;
- }
- $label;
- }
-
- sub tailmatch {
- local($string, $tail, $pos) = @_;
- $pos = length($string) - length($tail) + 1;
- if ($pos > 0 && substr($string, $pos, 999999) eq $tail) {
- return 1;
- }
- else {
- return 0;
- }
- }
-
- sub addud {
- local(*udlist, $name, $arg, $label, $S) = @_;
- $S = ' ' . $name . '{' . $label . '}';
- if (!&tailmatch($udlist{$arg}, $S)) {
- $udlist{$arg} = $udlist{$arg} . $S;
- }
- }
-
- sub listget {
- local($l, $i, $n, *a) = @_;
- $n = (@a = split(' ', $l));
- $a[$i];
- }
-
- sub uniqueid {
- local($name, $key) = @_;
- if ($uidtable{$name} eq '') {
- $key = &make_key($name);
- $key =~ s/[\]\[ \\{}`#%&~_^<>"\-]/*/g;
- $keycounts{$key} = $keycounts{$key} + 1;
- $uidtable{$name} = $key;
- if ($keycounts{$key} > 1) {
- $uidtable{$name} = $uidtable{$name} . '.' .
-
- &alphacode($keycounts{$key});
- }
- }
- $uidtable{$name};
- }
-
- sub make_key {
- local($name, $key, $l) = @_;
- $l = length($name);
- $name =~ s/^.*\///;
- $key = substr($name, 1, 3);
- if ($l >= 3) {
- $key = $key . &alphacode($l);
- }
- $key;
- }
-
- sub lognowebchunks {
- local($l, $j, $n, $X) = @_;
- if ($loggednowebchunks > 0) {
- return;
- }
- $loggednowebchunks = 1;
- delete $allchunks{0};
- $n = &alphasort(*allchunks);
- print '@xref beginchunks';
- for ($j = 0; $j < $n; $j++) {
- $name = $sorted{$j};
- delete $sorted{$j};
- printf "\@xref chunkbegin %s %s\n",
- ($anchorlabel{$name} ne '' ? $anchorlabel{$name} : 'nw\@notdef'),
-
- $name;
- $M = (@a = split(' ', $chunkud{$name}));
- for ($k = 1; $k <= $M; $k++) {
- if ($a[$k] =~ /^use/) {
- printf "\@xref chunkuse %s\n", substr($a[$k], 5,
-
- length($a[$k]) - 5);
- }
- elsif ($a[$k] =~ /^defn/) {
- printf "\@xref chunkdefn %s\n", substr($a[$k], 6,
-
- length($a[$k]) - 6);
- }
- }
- print '@xref chunkend';
- }
- print '@xref endchunks';
- }
-
- sub lognowebindex {
- local($l, $j, $n, $X) = @_;
- if ($loggednowebindex > 0) {
- return;
- }
- $loggednowebindex = 1;
- delete $allidents{0};
- $n = &alphasort(*allidents);
- print '@index beginindex';
- for ($j = 0; $j < $n; $j++) {
- $name = $sorted{$j};
- delete $sorted{$j};
- printf "\@index entrybegin %s %s\n",
- ($indexanchorlabel{$name} ne '' ? $indexanchorlabel{$name} :
-
- 'nw\@notdef'), $name;
- $M = (@a = split(' ', $indexud{$name}));
- for ($k = 1; $k <= $M; $k++) {
- if ($a[$k] =~ /^use/) {
- printf "\@index entryuse %s\n", substr($a[$k], 5,
-
- length($a[$k]) - 5);
- }
- elsif ($a[$k] =~ /^defn/) {
- printf "\@index entrydefn %s\n", substr($a[$k], 6,
-
- length($a[$k]) - 6);
- }
- }
- print '@index entryend';
- }
- print '@index endindex';
- }
-
- sub alphasort {
- local(*a, $X, $n) = @_;
- $n = 0;
- foreach $X ($[ .. $#a) {
- $n = &insertitem($X, $n);
- }
- $n;
- }
-
- sub insertitem {
- local($X, $n, $i, $tmp) = @_;
- $sorted{$n} = $X;
- $sortkeys{$n} = &sortkey($X);
- $i = $n;
- while ($i > 0 && ($sortkeys{$i} < $sortkeys{$i - 1} ||
- $sortkeys{$i} == $sortkeys{$i - 1} && $sorted{$i} lt $sorted{$i - 1})) {
- $tmp = $sortkeys{$i};
- $sortkeys{$i} = $sortkeys{$i - 1};
- $sortkeys{$i - 1} = $tmp;
- $tmp = $sorted{$i};
- $sorted{$i} = $sorted{$i - 1};
- $sorted{$i - 1} = $tmp;
- $i = $i - 1;
- }
- $n + 1;
- }
-
- sub sortkey {
- local($name, $S) = @_;
- $S = $name;
- $S =~ s/[^a-zA-Z ]//g;
- $S;
- }
-
- sub alphacode {
- local($n) = @_;
- if ($n < 0) {
- return '-' . &alphacode(-$n);
- }
- elsif ($n >= $alphacodelen) {
- return &alphacode($n / $alphacodelen) . &alphacode($n %
-
- $alphacodelen);
- }
- else {
- return substr($alphacodes, $n + 1, 1);
- }
- }
-